package org.ronghao.androidwifiplatfrom.activity;

import org.ronghao.androidwifiplatfrom.Facade;
import org.ronghao.androidwifiplatfrom.R;
import org.ronghao.androidwifiplatfrom.utils.AndroidUtil;
import org.ronghao.androidwifiplatfrom.utils.Contants;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;

public class LoginActivity extends Activity {
	private Button createGame_btn;
	private Button login_btn;
	private ImageView login_bg_imageView;
	private SharedPreferences sp;
	private android.view.View.OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.creategame_btn:
				Intent intent = new Intent(getApplicationContext(), CreateGameActivity.class);
				startActivity(intent);
				break;
			case R.id.logingame_btn:
				new Facade(getApplicationContext()).startWifi();
				Intent intent1 = new Intent(getApplicationContext(), GameHallActivity.class);
				startActivity(intent1);
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AndroidUtil.setNoTitleBar_Fullscreen(this);
		setContentView(R.layout.login);

		initData();
		initView();

		login_bg_imageView.setAnimation(loginAnimation());
	}

	private void initData() {
		//存储设置
		sp = getSharedPreferences(Contants.APP_LOCATION_SET, MODE_WORLD_READABLE);
		if (!sp.getBoolean(Contants.APP_SETTING_ISSETTED, false)) {
			Editor editor = sp.edit();
			editor.putString(Contants.COMMUNICATION_USER_NAME, "0");
			editor.putInt(Contants.COMMUNICATION_USER_STYLE, 0);
			editor.putInt(Contants.COMMUNICATION_USER_PIC, 0);
			editor.putString(Contants.COMMUNICATION_USER_IP, "127.0.0.1");
			editor.commit();
		}
	}

	private AlphaAnimation loginAnimation() {
		createGame_btn.setVisibility(View.INVISIBLE);
		login_btn.setVisibility(View.INVISIBLE);

		AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
		alphaAnimation.setDuration(3);// 设定动画时间
		alphaAnimation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				//TODO:进入动画MP4
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				login_bg_imageView.setVisibility(View.GONE);
				createGame_btn.setVisibility(View.VISIBLE);
				login_btn.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

		});
		return alphaAnimation;
	}

	private void initView() {
		createGame_btn = (Button) findViewById(R.id.creategame_btn);
		login_btn = (Button) findViewById(R.id.logingame_btn);
		login_bg_imageView = (ImageView) findViewById(R.id.login_bg_imageview);

		createGame_btn.setOnClickListener(onClickListener);
		login_btn.setOnClickListener(onClickListener);
	}
}
